package umputun.jutw.twitter;

import umputun.jutw.utils.logging.ULogger;
import winterwell.jtwitter.OAuthSignpostClient;
import winterwell.jtwitter.Status;
import winterwell.jtwitter.Twitter;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

public class TwitterReadOnly {

	public TwitterReadOnly(String name,
                           String consumer_key, String consumer_secret,
                           String access_tocken, String access_secret) {
        OAuthSignpostClient oauth = new OAuthSignpostClient(
//                OAuthSignpostClient.JTWITTER_OAUTH_KEY, OAuthSignpostClient.JTWITTER_OAUTH_SECRET,
                consumer_key, consumer_secret,
                access_tocken, access_secret
        );
		twitter = new Twitter(name, oauth);
		this.name = name;
		log.fine("RO Twitter created for userid="+name +
                ", ckey="+consumer_key +" ,csecret="+consumer_secret +
                ", akey="+access_tocken +", asecret="+access_secret);
	}

	public void setSinceId(BigInteger last_st) {
		this.last_st = last_st;
	}

	public Status getStatus() {
		return twitter.getStatus();
	}

	public List<Status> getUserTimeline() {
		List<Status> result = new ArrayList<Status>();
        try {
            List<Status> unfiltered = twitter.getUserTimeline(name);
            for (Status twst: unfiltered) {
                if (twst != null && twst.id.compareTo(last_st) >0)
                    result.add(twst);

            }
        }
        catch (Throwable ex) {
            log.warning("something wrong with twitter, err="+ex.getMessage());
        }
		return result;
	}

	public String getName() {
		return name;
	}

	private final String name;
	private final Twitter twitter;
	private BigInteger last_st = new BigInteger("0");
	private static final ULogger log = ULogger.getLogger();

}
